<template>
  <div :class="[isCollapse ? 'sub-menu-item-collapse' : 'sub-menu-item']">
    <!-- <i class="sub-icon" :class="'el-icon-document'" /> -->
    <svg-icon v-if="treeLevel == 1" class="sub-icon" :icon="menuIcon" />
    <div
      v-if="treeLevel != 1 || !isCollapse"
      class="sub-text"
      :title="title"
      :class="{ 'sub-text-active': activePath == name }"
    >
      <span>{{ title }}</span>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { pinyin } from "pinyin-pro";
export default {
  name: "MenuItem",
  props: {
    icon: {
      type: String || null,
      default: "",
    },
    treeLevel: {
      type: Number,
      default: 1,
    },
    name: {
      type: String,
      default: "",
    },
    title: {
      type: String,
      default: "",
    },
    isActive: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      activePath: "",
    };
  },
  computed: {
    ...mapGetters(["isCollapse"]),
    menuIcon() {
      if (this.icon && this.treeLevel == 1) {
        const pingyin = pinyin(this.title, { toneType: "none" }).replace(
          /\s/g,
          ""
        );
        return `menu-${pingyin}`;
      }
      return "";
    },
  },
  watch: {
    $route: {
      handler(to) {
        this.activePath = to.name;
      },
      immediate: true,
    },
  },
};
</script>

<style lang="scss" scoped>
.sub-menu-item {
  width: 100%;
  color: var(--main-color);
  display: flex;
  align-items: center;
}

.sub-menu-item-collapse {
  width: 100%;
  height: 56px;
  color: var(--main-color);
  display: grid;
  justify-items: center;
  align-items: center;
}

.sub-text {
  display: inline-block;
  width: 100px;
  margin-left: 6px;
  overflow: hidden; /* 确保超出容器的文本被隐藏 */
  white-space: nowrap; /* 确保文本在一行内显示 */
  text-overflow: ellipsis; /* 使用省略号表示被截断的文本 */
}

.sub-text-active {
  color: var(--main-hover);
}

.sub-icon {
  width: 20px;
  font-size: 20px;
  color: var(--main-color);

  &:hover {
    color: var(--main-hover);
  }
}

.sub-icon-active {
  color: var(--main-hover);
}

::v-deep {
  .el-submenu__icon-arrow {
    position: absolute;
    top: 50% !important;
    right: 20px;
    margin-top: -2px !important; /* 调整图标位置 */
    transition: transform 0.3s;
    font-size: 12px;
  }
}
</style>
